package Dailog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import student.andlei.com.student.R;
import utils.StringUtils;

/**
 * Created by xulei on 2017/12/6.
 */

public class InputDialog extends Dialog implements View.OnClickListener{
    private EditText contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;
    private int textcolor;
    private boolean hidecannle;
    public InputDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    public InputDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    public InputDialog(Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }
    public InputDialog(Context context, int themeResId,  OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.listener = listener;
    }
    protected InputDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public InputDialog setTitle(String title){
        this.title = title;
        return this;
    }

    public InputDialog setPositiveButton(String name){
        this.positiveName = name;
        return this;
    }

    public InputDialog setNegativeButton(String name){
        this.negativeName = name;
        return this;
    }
    public InputDialog settPositiveButtonColor(int textcolor){
        this.textcolor = textcolor;
        return this;
    }
    public InputDialog hideNegativeButton(){
        hidecannle = true;
        return this;
    }
    public InputDialog Enablefalse(){
        this.setCancelable(false);
        this.setCanceledOnTouchOutside(false);
        return this;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_input);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView(){
        contentTxt = (EditText)findViewById(R.id.content);
        titleTxt = (TextView)findViewById(R.id.title);
        submitTxt = (TextView)findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView)findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);
        if(hidecannle){
            cancelTxt.setVisibility(View.GONE);
        }
        contentTxt.setText(content);
        submitTxt.setTextColor(mContext.getResources().getColor(textcolor));
        if(!StringUtils.isNull(positiveName)){
            submitTxt.setText(positiveName);

        }

        if(!StringUtils.isNull(negativeName)){
            cancelTxt.setText(negativeName);
        }

        if(!StringUtils.isNull(title)){
            titleTxt.setText(title);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancel:
                if(listener != null){
                    listener.onClick(this, false);
                }
                this.dismiss();
                break;
            case R.id.submit:
                if(listener != null){
                    listener.onClick(this, true,contentTxt);
                }
                break;
        }
    }

    public interface OnCloseListener{
        void onClick(Dialog dialog, boolean confirm);
        void onClick(Dialog dialog, boolean confirm,EditText editText);
    }
}
